Event.Over = Class.create(
{
  initialize: function(element, overFn, outFn)
  {
    Event.observe(element, 'mouseover', function(event)
    {
      Element.addClassName(element, 'over');
      if (overFn)
        overFn(element);
    }.bind(this));
  
    Event.observe(element, 'mouseout', function(event)
    {
      Element.removeClassName(element, 'over');
      if (outFn)
        outFn(element);
    }.bind(this));
  }
});


Event.AjaxLoading = Class.create(
{
  initialize: function()
  {
    new Insertion.Top($$('body')[0], '<div id="ajax-loading">Loading...</div>');
    Element.hide('ajax-loading');

    Ajax.Responders.register(
    {
      onCreate: function()
      {
        Element.show('ajax-loading');
      },
    
      onComplete: function()
      {
        if (Ajax.activeRequestCount == 0)
        {
          Element.hide('ajax-loading');
        }
      }
    });
  }
});

var AjaxClient = Class.create(
{
  server: false,

  setServer: function(url)
  {
    this.server = url;

    new Event.AjaxLoading();
  },
  
  getServer: function()
  {
    return this.server;
  },

  ajaxUpdate: function(id, params, insertion)
  {
    if (!this.server)
    {
      alert('Server is not defined !');
      return;
    }
    var options = { method:       'get'
                  , parameters:   $H(params).toQueryString()
                  , evalScripts:  true
                  , onComplete:   this.ajaxResponse.bind(this)
                  , insertion:    insertion || false ? insertion : false
                  };

    new Ajax.Updater( id
                    , this.server
                    , options
                    );
  },
  
  ajaxRequest: function(params)
  {
    if (!this.server)
    {
      alert('Server is not defined !');
      return;
    }
    var options = { method:       'get'
                  , parameters:   $H(params).toQueryString()
                  , onComplete:   this.ajaxResponse.bind(this)
                  };

    new Ajax.Request( this.server
                    , options
                    );
  },
  
  ajaxInPlaceEditor: function(label, id_editor, params)
  {
    var aipeOptions = { clickToEditText: label
                      , okButton:        false
                      , evalScripts:     true
                      }; 
    new Ajax.InPlaceEditor( id_editor
                          , this.server + '?' + $H(params).toQueryString()
                          , aipeOptions
                          );
  },

  ajaxResponse : function(originalRequest)
  {
  }
});

Form.Validator = {
  _value: function(object)
  {
    return Object.isString(object) && (Try.these(function(){ return eval(object); }) || false);
  },
  
  is_int: function(object)
  {
    var v = this._value(object); 
    return Object.isNumber(v) && (v - Math.round(v) == 0); 
  },
  
  is_double: function(object)
  {
    return Object.isNumber(this._value(object));
  },
  
  is_string: function(object)
  {
    return Object.isString(object);
  }
};
